package com.ckzp.jfinal.system.controller;

import com.ckzp.core.model.Datas;
import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.plugins.layui.LayuiData;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.json.JsonUtil;
import com.ckzp.core.util.StringUtil;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Roles;
import com.ckzp.jfinal.system.model.*;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;
import java.util.Map;

/**
 * Created by 51594 on 2019/10/25.
 */
public class SysDeptController extends BaseController {

    public void index() {
        renderText("系统部门管理");
    }

    /**
     * 部门信息列表
     */
    @Admins(roles = "system", name = "部门信息列表", log = false)
    public void DeptList() {
        render("DeptList.html");
    }

    /**
     * 分类列表
     */
    @Admins(roles = "system", name = "添加部门页面", log = false)
    public void AddSystemDept() {
        FormData fd = FormData.dao().setActions("ADD");
        setAttr("dept", new SysDept());
        setAttr("form", fd);
        render("ModifySystemDept.html");
    }

    /**
     * 分类列表
     */
    @Admins(roles = "system", name = "修改部门页面", log = false)
    public void EditSystemDept() {
        String dept_id = getPara("dept_id", "");
        SysDept sr = SysDept.dao.findById(dept_id);
        FormData fd = FormData.dao().setActions("EDIT");
        setAttr("form", fd);
        setAttr("dept", sr);
        render("ModifySystemDept.html");
    }


    /**
     * 部门信息列表API
     */
    @Admins(roles = "system", name = "部门信息列表API", log = false)
    public void SysDeptListApi() {
        //////////////////
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_department").orderby("sindex");
        Page<SysDept> pages = ps.paginate(SysDept.dao);
        List datalist = pages.getList();
        renderJson(LayuiData.newData().setCount(pages.getTotalRow()).setData(datalist));
    }

    /**
     * 修改部门信息API
     */
    @Admins(roles = "system", name = "修改部门信息API", log = true)
    public void ModifySystemDeptApi() {
        Datas datas = getDatas(getPara("postdata"));
        String dept_id = datas.getStr("dept_id");
        String actions = datas.getStr("actions");
        if ("EDIT".equals(actions)) {
            boolean results = SysDept.dao.findById(dept_id)
                    .set("dept_name", datas.getStr("dept_name"))
                    .set("dept_desc", datas.getStr("dept_desc"))
                    .set("sindex", datas.getInt("sindex"))
                    .update();
            renderJson(Json(results, "修改"));
        } else {
            boolean results = new SysDept()
                    .set("dept_id", datas.getStr("dept_id"))
                    .set("dept_name", datas.getStr("dept_name"))
                    .set("dept_desc", datas.getStr("dept_desc"))
                    .set("sindex", datas.getInt("sindex"))
                    .save();
            renderJson(Json(results, "添加"));
        }
    }


    /**
     * 修改部门FieldAPI
     */
    @Admins(roles = "system", name = "修改部门FieldAPI", log = true)
    public void ModifySystemDeptFieldApi() {
        int dept_id = getParaToInt("key_id", 0);
        String field = getPara("field", "");
        String value = getPara("values", "");
        SysDept.dao.findById(dept_id).set(field, value).update();
        renderJson(JsonResult.n(0, "修改成功"));
    }

    /**
     * 检查登陆ID是否存在
     */
    @Admins(roles = "system", name = "检查登陆ID是否存在", log = true)
    public void CheckDeptIdApi() {
        int dept_id = getParaToInt("dept_id", 0);
        String sql = "select dept_id from sys_department where dept_id=?";
        SysDept dept = SysDept.dao.findFirst(sql, dept_id);
        if (dept != null && dept.getStr("dept_id") != null) {
            renderJson(JsonResult.n(9, "部门编号重复"));
        } else {
            renderJson(JsonResult.n(0, "部门编号不存在"));
        }
    }

    /**
     * 删除部门API
     */
    @Admins(roles = "system", name = "删除部门API", log = true)
    public void DelSystemDeptApi() {
        int dept_id = getParaToInt("dept_id", 0);
        int counts = Db.queryInt("select count(*) from sys_user where dept_id=?", dept_id);
        if (counts == 0) {
            if (SysDept.dao.findById(dept_id).delete()) {
                renderJson(JsonResult.n(0, "删除成功"));
            } else {
                renderJson(JsonResult.n(9, "删除失败"));
            }
        } else {
            renderJson(JsonResult.n(9, "该部门下有" + counts + "用户,不能删除"));
        }
    }
}